﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace SplitAndEncryptFiles
{
    class SplitAndEncrypt
    {
        public const string FILENAME = "Test.mp3";
        public const int ONE_MB = 1024 * 1024;

        public static void SplitAndEncryptFile(string fileName, string password)
        {
            using (FileStream readStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            { 
                int partNumber=1;
                while (true)
                {
                    byte[] buffer = new byte[ONE_MB];
                    byte[] bufferRead;
                    int bytesRead = readStream.Read(buffer, 0, ONE_MB);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    if (bytesRead != ONE_MB)
                    {

                        bufferRead = new byte[bytesRead];
                        Array.Copy(buffer, bufferRead, bytesRead);
                    }
                    else
                    {
                        bufferRead = buffer;
                    }
                    byte[] encryptedBytes = EncryptionWithStreams.EncryptBytes(bufferRead, password);
                    string newFilename = string.Format("{0}-part{1:000}.{2}",
                        fileName.Split('.')[0], partNumber, fileName.Split('.')[1]);
                    using (FileStream writeStream = new FileStream(newFilename, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        writeStream.Write(encryptedBytes, 0, bytesRead);
                    }
                    partNumber++;
                }
            }
        }

        public static void MergeFilesToOriginal(string fileName, string password)
        {
            string[] nameParts = fileName.Split('.');
            string outputFilename = string.Format("{0}-original.{1}", nameParts[0], nameParts[1]);
            using (FileStream writeStream = new FileStream(outputFilename, FileMode.OpenOrCreate, FileAccess.Write))
            {
                int partNumber = 1;
                while (true)
                {
                    string nextFileName = string.Format("{0}-part{1:000}.{2}",
                            fileName.Split('.')[0], partNumber, fileName.Split('.')[1]);
                    if (!File.Exists(nextFileName))
                    {
                        break;
                    }
                    using (FileStream readStream = new FileStream(nextFileName, FileMode.Open, FileAccess.Read))
                    {
                        byte[] buffer = new byte[ONE_MB];
                        byte[] bufferRead;
                        int bytesRead = readStream.Read(buffer, 0, ONE_MB);
                        if (bytesRead != ONE_MB)
                        {

                            bufferRead = new byte[bytesRead];
                            Array.Copy(buffer, bufferRead, bytesRead);
                        }
                        else
                        {
                            bufferRead = buffer;
                        }
                        byte[] decryptedBytes = EncryptionWithStreams.DecryptBytes(bufferRead, password);
                        writeStream.Write(decryptedBytes, 0, bytesRead);
                    }
                    partNumber++;
                }
            }
        }

        static void Main()
        {
            string password = "asd";
            SplitAndEncryptFile(FILENAME, password);
            MergeFilesToOriginal(FILENAME, password);
        }
    }
}
